
#include "stdio.h"
#include "conio.h"
#include <winsock.h>
//#include "Socket.h"
//#include <winsock.h>
#define USE_MY_CODE
////////////////////////////////////
//          CLIENT                //
////////////////////////////////////
//#define PRINTERROR(s) fprintf(stderr,"\n%: %d\n", s, WSAGetLastError())
void StreamClient(char *szServer, short nPort);
void main(int argc, char **argv)
{
	WORD wVersionRequested = MAKEWORD(1,1);
	WSADATA wsaData;
	int nRet;
	short nPort;

	//
	// Check for the host and port arguments
	//
#ifndef USE_MY_CODE
	if (argc != 3)
	{
		fprintf(stderr,"\nSyntax: client ServerName PortNumber\n");
		return;
	}

	nPort = atoi(argv[2]);
#endif

	//
	// Initialize WinSock and check the version
	//
	nRet = WSAStartup(wVersionRequested, &wsaData);
	if (wsaData.wVersion != wVersionRequested)
	{	
		fprintf(stderr,"\n Wrong version\n");
		return;
	}


	//
	// Go do the stuff a stream client does
	//
#ifndef USE_MY_CODE
	StreamClient(argv[1], nPort);
#else
	///////////////////////////////////////////////////////////////////
	char * szServer = "127.0.0.1";
	nPort = 1000;
	printf("\nStream Client connecting to server: %s on port: %d",
		szServer, nPort);

	//
	// Find the server
	//
	LPHOSTENT lpHostEntry;

	lpHostEntry = gethostbyname(szServer);
	if (lpHostEntry == NULL)
	{
		printf("Can't find server");
		return;
	}
	bool isRunning = false;
	//
	// Create a TCP/IP stream socket
	//
	SOCKET	theSocket;

	theSocket = socket(AF_INET,// Address family
		SOCK_STREAM,// Socket type
		IPPROTO_TCP);		// Protocol
	if (theSocket == INVALID_SOCKET)
	{
		//PRINTERROR("socket()");
		return;
	}

	//
	// Fill in the address structure
	//
	SOCKADDR_IN saServer;

	saServer.sin_family = AF_INET;

	// Server's address

	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
	saServer.sin_port = htons(nPort);	// Port number from command line

	//
	// connect to the server
	//
	//int nRet;

	nRet = connect(theSocket,	// Socket
		(LPSOCKADDR)&saServer,	// Server address
		sizeof(struct sockaddr));// Length of server address structure
	if (nRet == SOCKET_ERROR)
	{
		printf("Connect error!!!");
		closesocket(theSocket);
		return;
	}


	//
	// Send data to the server
	//
	char szBuf[256];
	isRunning = true;
	while(isRunning)
	{
		printf("\nSend string: ");
		gets(szBuf);
		//scanf("%s\n",szBuf);
		nRet = send(theSocket,// Connected socket
			szBuf,	// Data buffer
			strlen(szBuf),	// Length of data
			0);	// Flags
		if(strcmp(szBuf, "exit") == 0)
		{
			isRunning = false;
			break;
		}
		if (nRet == SOCKET_ERROR)
		{
			printf("!!!! send error !!!!!");
			closesocket(theSocket);
			return;
		}

		nRet = recv(theSocket, // Connected socket
			szBuf,	// Receive buffer
			sizeof(szBuf),	// Size of receive buffer
			0);	// Flags
		if (nRet == SOCKET_ERROR)
		{
			//PRINTERROR("recv()");
			closesocket(theSocket);
			return;
		}


		//
		// Display the received data
		//

		szBuf[nRet]=0;
		printf("%s", szBuf);
		printf("\n-------\n");
	}
	closesocket(theSocket);
/*
	scanf("%s",szBuf);
	printf("Send string: %s",szBuf);
	//strcpy(szBuf, "From the Client");
	nRet = send(theSocket,// Connected socket
		szBuf,	// Data buffer
		strlen(szBuf),	// Length of data
		0);	// Flags
	if (nRet == SOCKET_ERROR)
	{
		//PRINTERROR("send()");
		closesocket(theSocket);
		return;
	}
*/
	////////////////////////////////////////////////////////////////////
#endif


	//
	// Release WinSock
	//
	WSACleanup();
}

void StreamClient(char *szServer, short nPort)
{
	printf("\nStream Client connecting to server: %s on port: %d",
		szServer, nPort);

	//
	// Find the server
	//
	LPHOSTENT lpHostEntry;

	lpHostEntry = gethostbyname(szServer);
	if (lpHostEntry == NULL)
	{
		//PRINTERROR("gethostbyname()");
		return;
	}

	//
	// Create a TCP/IP stream socket
	//
	SOCKET	theSocket;

	theSocket = socket(AF_INET,// Address family
					SOCK_STREAM,// Socket type
					IPPROTO_TCP);// Protocol
	if (theSocket == INVALID_SOCKET)
	{
		//PRINTERROR("socket()");
		return;
	}

	//
	// Fill in the address structure
	//
	SOCKADDR_IN saServer;

	saServer.sin_family = AF_INET;

	// Server's address
	saServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
	saServer.sin_port = htons(nPort);	// Port number from command line

	//
	// connect to the server
	//
	int nRet;

	nRet = connect(theSocket,	// Socket
		(LPSOCKADDR)&saServer,	// Server address
		sizeof(struct sockaddr));// Length of server address structure
	if (nRet == SOCKET_ERROR)
	{
		//PRINTERROR("socket()");
		closesocket(theSocket);
		return;
	}


	//
	// Send data to the server
	//
	char szBuf[256];

	strcpy(szBuf, "From the Client");
	nRet = send(theSocket,// Connected socket
		szBuf,	// Data buffer
		strlen(szBuf),	// Length of data
		0);	// Flags
	if (nRet == SOCKET_ERROR)
	{
		//PRINTERROR("send()");
		closesocket(theSocket);
		return;
	}


	//
	// Wait for a reply
	//
	nRet = recv(theSocket, // Connected socket
		szBuf,	// Receive buffer
		sizeof(szBuf),	// Size of receive buffer
		0);	// Flags
	if (nRet == SOCKET_ERROR)
	{
		//PRINTERROR("recv()");
		closesocket(theSocket);
		return;
	}


	//
	// Display the received data
	//

	szBuf[nRet]=0;
	printf("\nData received: %s", szBuf);


	closesocket(theSocket);
	return;
}